//
//  RightViewController.m
//  课余时间
//
//  Created by zgr on 16/4/6.
//  Copyright © 2016年 张大少. All rights reserved.
//

#import "RightViewController.h"
#import "GameViewController.h"
#import "TraveMainViewController.h"
#import "NewsViewController.h"
#import "ReadViewController.h"
@interface RightViewController ()
@property (nonatomic , strong)UITapGestureRecognizer *tap;
@property (nonatomic, strong)UIViewController *listViewVc;
@property (nonatomic ,strong)UINavigationController *nav;

@end

@implementation RightViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
 
    
    
    
    _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    
    _listViewVc  = [[NSClassFromString(@"TraveMainViewController") alloc]init];
     _nav = [[UINavigationController alloc]initWithRootViewController:_listViewVc];
   // [self addChildViewController:nav];
    [self.view addSubview:_nav.view];
    
    
    UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgePan:)];
    edgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:edgePan];
    
    [self.view addSubview:_listViewVc.view];

    
}
-(void)edgePan:(UIScreenEdgePanGestureRecognizer *)sender
{
    CGPoint point = [sender locationInView:self.view.superview];
    NSLog(@"%f",point.x);
    
    CGRect newFrame = self.view.frame;
    switch (sender.state) {
        case UIGestureRecognizerStateEnded:
        {
            if (point.x < 100) {
                point.x = 0;
            }
            else
            {
                point.x = KWIDTH - 175;
                [self.view addGestureRecognizer:_tap];
            }
            newFrame.origin.x = point.x;
            [UIView animateWithDuration:0.5 animations:^{
                self.view.frame = newFrame;
            }];
            
            
            
            break;
        }
            case UIGestureRecognizerStateChanged:
        {
            if (point.x > KWIDTH - 175) {
                point.x = KWIDTH - 175;
            }
            break;
        }
        default:
            break;
    }
    newFrame.origin.x = point.x ;
    self.view.frame = newFrame;
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT);
    }];
    [self.view removeGestureRecognizer:tap];
}

-(void)changViewWithIndex:(NSInteger)index
{
    
    NSArray *array = @[@"GameViewController",@"TraveMainViewController",@"NewsViewController",@"ReadViewController"];
    if ([_listViewVc isMemberOfClass:NSClassFromString(array[index])]) {
        return;
    }
   
    [_listViewVc.view removeFromSuperview];
    if ([array[index] isEqualToString:@"NewsViewController"]) {
        _listViewVc = [[NewsViewController alloc]initWithNibName:@"NewsVc" bundle:nil];
        // _nav = [[UINavigationController alloc] initWithRootViewController:_listViewVc];
        
    }
    
    else{
    _listViewVc = [[NSClassFromString(array[index]) alloc]init];
    }
    
    if ([array[index] isEqualToString:@"TraveMainViewController"]||[array[index] isEqualToString:@"NewsViewController"]) {
      _nav = [[UINavigationController alloc]initWithRootViewController:_listViewVc];
        
        [self.view addSubview:_nav.view];
    }
    else
    {
    [self.view addSubview:_listViewVc.view];
    }
    
    if ([array[index]isEqualToString:@"ReadViewController"])
    {
         _nav = [[UINavigationController alloc]initWithRootViewController:_listViewVc];
         // _listViewVc = [[NSClassFromString(array[index])alloc] init];
        [self.view addSubview:_nav.view];
    }
    else
    {
        [self.view addSubview:_listViewVc.view];
    }
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
